﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.CharacterDevelopment.CharacterPerks
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using TaleWorlds.Core;
using TaleWorlds.ObjectSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.CharacterDevelopment
{
  public class CharacterPerks : PropertyOwner<PerkObject>
  {
    internal static void AutoGeneratedStaticCollectObjectsCharacterPerks(
      object o,
      List<object> collectedObjects)
    {
      ((MBObjectBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    public CharacterPerks()
    {
    }

    public CharacterPerks(CharacterPerks perks)
      : base((PropertyOwner<PerkObject>) perks)
    {
    }

    public void ClearChangedPerks(Hero owner)
    {
      foreach (PerkObject attribute in this._attributes.Keys.ToList<PerkObject>())
      {
        if (attribute == null || attribute.IsTrash || (double) owner.GetSkillValue(attribute.Skill) < (double) attribute.RequiredSkillValue)
          this.SetPropertyValue(attribute, 0);
      }
    }

    public override void Deserialize(MBObjectManager objectManager, XmlNode node)
    {
      this.Initialize();
      foreach (XmlNode childNode in node.ChildNodes)
      {
        if (childNode.NodeType != XmlNodeType.Comment)
        {
          XmlAttributeCollection attributes = childNode.Attributes;
          string objectName = attributes["id"].Value;
          string str = attributes["value"].Value;
          PerkObject attribute = Game.Current.ObjectManager.GetObject<PerkObject>(objectName);
          if (attribute != null)
          {
            int num = str != null ? Convert.ToInt32(str) : 1;
            this.SetPropertyValue(attribute, num);
          }
        }
      }
      foreach (XmlNode childNode in node.ChildNodes)
      {
        string objectName = childNode.Name;
        int num = 1;
        if (objectName == "Perk")
        {
          objectName = (string) null;
          if (childNode.Attributes != null)
          {
            XmlAttribute attribute = childNode.Attributes["id"];
            if (attribute != null)
              objectName = attribute.InnerText;
          }
        }
        if (objectName != null)
        {
          PerkObject attribute1 = Game.Current.ObjectManager.GetObject<PerkObject>(objectName);
          if (attribute1 != null)
          {
            XmlAttribute attribute2 = childNode.Attributes["value"];
            if (attribute2 != null)
            {
              string str = attribute2.Value;
              num = str != null ? Convert.ToInt32(str) : 1;
            }
            this.SetPropertyValue(attribute1, num);
          }
        }
      }
    }
  }
}
